import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

/* Router Modules */
import RouterLayout from '@/views/layout/RouterLayout.vue'

// 关联homeTab的route
export const homeTabRouterMap = [
  // 首页
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/home/index'),
    meta: {
      title: '首页',
      inactive: 'images/common/home_tabbar/home.png',
      active: 'images/common/home_tabbar/home_active.png'
    }
  },
  // 课程
  {
    path: '/course',
    name: 'Course',
    component: () => import('@/views/course'),
    meta: {
      title: '课程',
      inactive: 'images/common/home_tabbar/community.png',
      active: 'images/common/home_tabbar/community_active.png',
      roles: true
    }
  },
  // 我的
  {
    path: '/mine',
    name: 'Mine',
    component: () => import('@/views/mine/index'),
    meta: {
      title: '我的',
      roles: false,
      inactive: 'images/common/home_tabbar/mine.png',
      active: 'images/common/home_tabbar/mine_active.png'
    }
  }
]

// 通用的route
export const constantRouterMap = [
  // 登录
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' }
  },
  // 课程详情
  {
    path: '/course/detail',
    name: 'CourseDetail',
    component: () => import('@/views/course/detail'),
    meta: { title: '课程详情' }
  },
  // 作业详情
  {
    path: '/homework',
    name: 'Homework',
    component: () => import('@/views/homework'),
    meta: { title: '作业详情' }
  },
  // 个人信息详情
  {
    path: '/mine/mineCenter',
    component: RouterLayout,
    children: [
      {
        path: '',
        redirect: {
          path: '/404'
        }
      },
      {
        path: 'student',
        name: 'MineCenterStudent',
        component: () => import ('@/views/mine/mineCenter/student'),
        meta: { title: '学生个人中心' }
      },
      {
        path: 'teacher',
        name: 'MineCenterTeacher',
        component: () => import ('@/views/mine/mineCenter/teacher'),
        meta: { title: '教师个人中心' }
      },
      {
        path: 'settings',
        name: 'MineSettings',
        component: () => import('@/views/mine/settings'),
        meta: { title: '设置' }
      },
      {
        path: 'teacher/editInfo',
        name: 'MineCenterTeacherEditInfo',
        component: () => import ('@/views/mine/mineCenter/teacher/editInfo'),
        meta: { title: '教师个人信息编辑' }
      },
      {
        path: 'student/editInfo',
        name: 'MineCenterStudentEditInfo',
        component: () => import ('@/views/mine/mineCenter/student/editInfo'),
        meta: { title: '学生个人信息编辑' }
      }
    ]

  },
  // 组织模块 页面
  {
    path: '/organization',
    component: RouterLayout,
    meta: { roles: false },
    children: [
      {
        path: '',
        redirect: {
          path: '/404'
        }
      },
      {
        path: 'classroom',
        name: 'OrganizationClassroom',
        component: () => import ('@/views/organization/classroom'),
        meta: { title: '教室信息' }
      }
    ]
  },
  // 课程模块 页面
  {
    path: '/course',
    component: RouterLayout,
    meta: { roles: false },
    children: [
      {
        path: 'overview',
        name: 'CourseOverview',
        component: () => import('@/views/course/overview'),
        meta: { title: '课程信息', roles: false }
      }
    ]
  },
  // 作业模块 页面
  {
    path: '/homework/question',
    name: 'HomeworkQuestion',
    component: () => import('@/views/homework/question'),
    meta: { title: '作业题目列表' }
  },
  // 作业提交列表 页面
  {
    path: '/homework/check',
    name: 'HomeworkCheck',
    component: () => import('@/views/homeworkCheck'),
    meta: { title: '作业提交列表' }
  },
  // 课表 页面
  {
    path: '/courseTable',
    name: 'courseTable',
    component: () => import('@/views/courseTable'),
    meta: { title: '个人课表' }
  },

  // -------
  // 搜索页面
  {
    path: '/search/house',
    name: 'SearchHouse',
    component: () => import('@/views/search/house'),
    meta: { title: '搜索-房源' }
  },
  // 房屋列表
  {
    path: '/house/list',
    name: 'HouseList',
    component: () => import('@/views/house/list'),
    meta: { title: '房屋列表' }
  },
  // 房屋详情
  {
    path: '/house/detail',
    name: 'HouseDetail',
    component: () => import('@/views/house/detail'),
    meta: { title: '房屋详情' }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404'),
    meta: { title: '404' }
  },
  // 404 页面必须放在最后面
  { path: '*', redirect: '/404', hidden: true }
]

// 需要进行权限控制的route
export const asyncRouterMap = []

function allRouteMap() {
  return [...homeTabRouterMap, ...constantRouterMap, ...asyncRouterMap]
}

// 获取页脚导航菜单列表
export function getHomeTabList() {
  const list = []
  homeTabRouterMap.forEach(r => {
    list.push({
      path: r.path,
      title: r.meta.title,
      inactive: r.meta.inactive,
      active: r.meta.active
    })
  })
  return list
}

// 获取白名单
export function getWhiteList() {
  return allRouteMap().map((route) => {
    if (route.meta && !route.meta.roles) {
      return route.path
    }
  })
}

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    routes: allRouteMap(),
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition
      } else {
        return {
          x: 0,
          y: 0
        }
      }
    }
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
